package com.codescholar.file;

import com.codescholar.utils.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.sql.Timestamp;

public class FileReversal {
    public static void main(String[] args) throws IOException {
        // 源文件
        String path = "a.md";
        // 读取的行
        String line;

        // 面试问题
        String question = "";
        // 答案
        String answer = "";

        String path2 = "b.md";

        Timestamp createTime = new Timestamp(System.currentTimeMillis());
        Timestamp updateTime = new Timestamp(System.currentTimeMillis());

        //1.导入jar包
        //2.创建JDBCTemplate对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        //3.调用方法
        String sql = "insert into e_exercise(answer, create_time, question, update_time, views, type_id, user_id) values( ?, ?, ?, ?, 0, 2, 1)";


        File file = new File(path);
        File file2 = new File(path2);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(file2));


        while ((line = reader.readLine()) != null){
            if (line.length()>1){
                if (line.substring(0,2).equals("##")){

                    //System.out.println(answer);
                    question = line+"\r\n";

                    int count = template.update(sql, answer, createTime, question, updateTime);
                    System.out.println(count);
                    //System.out.println(question);

                    answer = "";
                }else {
                    answer += line+"\r\n";
                }
            }else {
                answer += "\r\n";
            }
        }

        reader.close();
        writer.close();
    }
}
